import 'package:flutter/material.dart';
import 'package:flutter_trip/dao/travel_tab_dao.dart';
import 'package:flutter_trip/page/travel_tab_page.dart';

import '../model/travel_tab_model.dart';

class TravelPage extends StatefulWidget {
  const TravelPage({super.key});

  @override
  State<TravelPage> createState() => _TravelPageState();
}

class _TravelPageState extends State<TravelPage>
    with TickerProviderStateMixin {
  late TabController _controller = TabController(length: 0, vsync: this);
  List<TravelTab>? tabs = [];
  late TravelTabModel travelModel;

  @override
  void initState() {
    super.initState();
    TravelTabDao.fetch().then((TravelTabModel model) {
      _controller = TabController(length: model.tabs?.length ?? 0, vsync: this);
      setState(() {
        tabs = model.tabs;
        travelModel = model;
      });
    }).catchError((e) {
      print(e);
    });
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Container(
            color: Colors.white,
            padding: const EdgeInsets.only(top: 30),
            child: TabBar(
              tabs: tabs!.map<Tab>((TravelTab item) {
                return Tab(
                  text: item.labelName,
                );
              }).toList(),
              controller: _controller,
              isScrollable: true,
              labelColor: Colors.black,
              labelPadding: const EdgeInsets.fromLTRB(20, 0, 10, 5),
              indicator: const UnderlineTabIndicator(
                borderSide: BorderSide(
                  color: Color(0xFF2fcfbb),
                  width: 3,
                ),
                insets: EdgeInsets.only(bottom: 10),
              ),
            ),
          ),
          Flexible(
              child: TabBarView(
                  controller: _controller,
                  children: tabs!.map<TravelTabPage>((TravelTab item) {
                    return TravelTabPage(
                        travelUrl: travelModel.url!,
                        groupChannelCode: item.groupChannelCode!);
                  }).toList())),
        ],
      ),
    );
  }
}
